package com.xnyzc.lhy.order.service.impl.area;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.penum.area.EIsOpne;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.ResultListUtil;
import com.xnyzc.lhy.order.entity.area.OaSysOpenArea;
import com.xnyzc.lhy.order.mapper.area.OaSysOpenAreaMapper;
import com.xnyzc.lhy.order.service.area.IOaSysOpenAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  开通城市实现类
 * </p>
 *
 * @author zhuyubin
 * @since 2019-08-20
 */
@Service
public class OaSysOpenAreaServiceImpl extends ServiceImpl<OaSysOpenAreaMapper, OaSysOpenArea> implements IOaSysOpenAreaService {

    @Autowired
    private OaSysOpenAreaMapper oaSysOpenAreaMapper;

    /**
     * 获取租户开通城市
     * @param tag 租户
     * @param search 搜索
     * @return
     */
    @Override
    public List<Map<String, Object>> selectOpenArea(String tag, String search){
        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, Object> map = new HashMap<>(16);
        // 查询条件
        LambdaQueryWrapper<OaSysOpenArea> eq = Wrappers.<OaSysOpenArea>lambdaQuery()
                .eq(OaSysOpenArea::getTag, tag)
                .eq(OaSysOpenArea::getIsOpne, EIsOpne.yes.getValue());
        // 无查询条件
        if (CheckUtil.objIsEmpty(search)){
            List<OaSysOpenArea> oaSysOpenAreas = oaSysOpenAreaMapper.selectList(eq);
            if (CheckUtil.objIsEmpty(oaSysOpenAreas)){
                return result;
            }
            // 首个节点
            String cityPyname = oaSysOpenAreas.get(0).getCityPyname();
            String titleSmall = cityPyname.substring(0, 1);
            String title = titleSmall.toUpperCase();
            map.put("type", 1);
            map.put("name", title);
            map.put("id", "-1");
            result.add(map);
            for (OaSysOpenArea oaSysOpenArea : oaSysOpenAreas) {
                map = new HashMap<>(16);
                cityPyname = oaSysOpenArea.getCityPyname();
                if (CheckUtil.objIsEmpty(cityPyname)){
                    continue;
                }
                String titleSmallInside = cityPyname.substring(0, 1);
                String titleInside = titleSmallInside.toUpperCase();
                if (title.equals(titleInside)){
                    map.put("type", 0);
                    map.put("name", this.isNull(oaSysOpenArea.getCityName()));
                    map.put("id", this.isNull(oaSysOpenArea.getCityId()));
                } else {
                    title = titleInside;
                    map.put("type", 1);
                    map.put("name", title);
                    map.put("id", "-1");
                    result.add(map);
                    map = new HashMap<>(16);
                    map.put("type", 0);
                    map.put("name", this.isNull(oaSysOpenArea.getCityName()));
                    map.put("id", this.isNull(oaSysOpenArea.getCityId()));
                }
                result.add(map);
            }
            return result;
        }
        // 中文查询
        if (CheckUtil.isUtf8(search)){
            eq.like(OaSysOpenArea::getCityName, search);
            return this.selectNoTitle(eq, map, result);
        }
        // 拼音查询
        else {
            eq.likeRight(OaSysOpenArea::getCityPyname, search);
            return this.selectNoTitle(eq, map, result);
        }
    }

    /**
     * 空处理
     * @param value
     * @return
     */
    private String isNull(Object value){
        return CheckUtil.objIsEmpty(value) ? "" : value.toString();
    }

    /**
     * 查询
     * @param eq 条件
     * @param map 内置map
     * @param result 出参
     * @return
     */
    private List<Map<String, Object>> selectNoTitle(LambdaQueryWrapper<OaSysOpenArea> eq, Map<String, Object> map, List<Map<String, Object>> result){
        List<OaSysOpenArea> oaSysOpenAreas = oaSysOpenAreaMapper.selectList(eq);
        if (CheckUtil.objIsEmpty(oaSysOpenAreas)){
            return result;
        }
        for (OaSysOpenArea oaSysOpenArea : oaSysOpenAreas) {
            map = new HashMap<>(16);
            map.put("type", 0);
            map.put("name", this.isNull(oaSysOpenArea.getCityName()));
            map.put("id", this.isNull(oaSysOpenArea.getCityId()));
            result.add(map);
        }
        return result;
    }
}
